#include "hellomqtt.h"
#include "ui_hellomqtt.h"
#include "./include/qmqtt.h"
#include <qnetwork.h>
#include <QIcon>

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

hellomqtt::hellomqtt(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::hellomqtt)
{
    qDebug() << "client init";

    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/res/res/smarthome.png"));

    QImage *img=new QImage;   //新建一个image对象

    img->load(":/res/res/temperature.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
    ui->temp_icon->setPixmap(QPixmap::fromImage(*img)); //将图片放入label，使用setPixmap,注意指针*img
    img->load(":/res/res/humidity.png"); //将图像资源载入对象img，注意路径，可点进图片右键复制路径
    ui->humi_icon->setPixmap(QPixmap::fromImage(*img));

    delete img;

    client = new QMQTT::Client(QHostAddress("192.168.9.162"), 1883); //创建MQTT客户端

    connect(client, &QMQTT::Client::connected, this, &hellomqtt::mqttconnect);
    connect(client, &QMQTT::Client::received, this, &hellomqtt::HandlerJsonMessage);

//    client->setClientId("clientId");
//    client->setUsername("user");
//    client->setPassword("password");
    client->connectToHost();            //连接mqtt服务器

}

hellomqtt::~hellomqtt()
{
    delete ui;
}

void hellomqtt::mqttconnect()
{
    client->subscribe("control", 2);    //订阅主题
    client->subscribe("sensor", 2);     //订阅主题
    qDebug() << "mqtt connect";
}

void hellomqtt::SendJsonMessege(QString name, char &flag)
{
    QString string;

    if (flag  == 0)
    {
        string = "ON";
        flag = 1;
    }
    else
    {
        string = "OFF";
        flag = 0;
    }
    QJsonObject obj;
    obj.insert(name, string);
    QJsonDocument doc;
    doc.setObject(obj);
    QByteArray str = doc.toJson(QJsonDocument::Compact);

//    arr.append(string);
    QMQTT::Message message;
    message.setTopic("control");
    message.setPayload(str);
    message.setQos(2);

    client->publish(message);
}

void hellomqtt::HandlerJsonMessage(const QMQTT::Message& message)
{
    QJsonParseError err;
    QByteArray arr(message.payload());
    QJsonDocument  rootDoc = QJsonDocument::fromJson(arr, &err); // 字符串格式化为JSO*/N

    qDebug() << "get json message" << message.payload();

    if(err.error != QJsonParseError::NoError)
    {
        qDebug() << "JSON格式错误";
        return;
    }
    else
    {
        QJsonObject rootObj = rootDoc.object();       //获取根节点
        QJsonValue val = rootObj.value("temperature");
        qDebug() << "temperature" << val.toString();
        ui->temp_text->setText(val.toString() + " ℃");
        val = rootObj.value("humidity");
        qDebug() << "humidity" << val.toString();
        ui->humi_text->setText(val.toString() + " %RH");
    }
}

void hellomqtt::on_LightButton_clicked()
{
    static char flag = 0;

    if (flag == 0)
    {
        ui->LightButton->setIcon(QIcon(":/res/res/light_on.png"));
    }
    else
    {
         ui->LightButton->setIcon(QIcon(":/res/res/light.png"));
    }

    SendJsonMessege("led",  flag);
}

void hellomqtt::on_DoorButton_clicked()
{
    static char flag = 0;

    if (flag == 0)
    {
        ui->DoorButton->setIcon(QIcon(":/res/res/memjin_on.png"));
    }
    else
    {
         ui->DoorButton->setIcon(QIcon(":/res/res/menjin.png"));
    }

    SendJsonMessege("door", flag);
}

void hellomqtt::on_FanButton_clicked()
{
    static char flag = 0;

    if (flag == 0)
    {
        ui->FanButton->setIcon(QIcon(":/res/res/fan_on.png"));
    }
    else
    {
        ui->FanButton->setIcon(QIcon(":/res/res/fan.png"));
    }

    SendJsonMessege("fan", flag);
}

void hellomqtt::on_SocketButton_clicked()
{
    static char flag = 0;

    if (flag == 0)
    {
        ui->SocketButton->setIcon(QIcon(":/res/res/socket_on.png"));
    }
    else
    {
        ui->SocketButton->setIcon(QIcon(":/res/res/socket.png"));
    }

    SendJsonMessege("socket", flag);
}

void hellomqtt::on_TvButton_clicked()
{
    static char flag = 0;

    if (flag == 0)
    {
        ui->TvButton->setIcon(QIcon(":/res/res/tv_show_on.png"));
    }
    else
    {
        ui->TvButton->setIcon(QIcon(":/res/res/tv_show.png"));
    }

    SendJsonMessege("socket", flag);
}

void hellomqtt::on_CurtainButton_clicked()
{
    static char flag = 0;

    if (flag == 0)
    {
        ui->CurtainButton->setIcon(QIcon(":/res/res/curtain_on.png"));
    }
    else
    {
        ui->CurtainButton->setIcon(QIcon(":/res/res/curtain.png"));
    }

    SendJsonMessege("socket",  flag);
}
